/*
 * @Description: 全宗reducer
 * @Author: Rfan
 * @Date: 2022-01-08 16:30:05
 * @LastEditTime: 2022-09-05 18:18:35
 */

import { arrToTree, jsonClone } from '@/utils';
import { delItemByIdArray, editItemById, insertItem, insertItemAfter } from '@/utils/list';
import {
  ADD_DEPARTMENT,
  ADD_USER,
  ARC_TYPE_LOADING,
  CLEAR_SELECT_USER,
  DELETE_DEPARTMENT,
  DELETE_USER,
  EDIT_DEPARTMENT,
  SET_DEPARTMENT_LIST,
  SET_QUERY,
  SET_SELECT_DEPARTMENT,
  SET_SELECT_USER_ID,
  SET_USER_LIST,
  SHOW_USER_LIST_LOADING
} from '../constants';

export const OrgAndUserReducer = (state, action) => {
  switch (action.type) {
    // 表格加载loading
    case ARC_TYPE_LOADING:
      return { ...state, loading: true };

    /**
     * @description: 加载表格数据及相关数据
     * @param {Array} data 表格数据
     * @return {*}
     */
    case SET_DEPARTMENT_LIST: {
      return {
        ...state,
        departmentList: action.data,
        departmentTree: arrToTree(action.data)
      };
    }

    /**
     * @description: 设置选中部门
     * @param {Object} data 选中的部门
     */
    case SET_SELECT_DEPARTMENT:
      return {
        ...state,
        selectDepartment: action.data
      };

    /**
     * @description: 设置选中用户ID
     * @param {Object} id 选中用户的ID
     */
    case SET_SELECT_USER_ID: {
      const list = jsonClone(state.selectUserIds);
      let selectUserIds = [];
      if (action.way) {
        list.push(action.id);
        selectUserIds = list;
      } else {
        for (let i = 0; i < list.length; i++) {
          if (action.id === list[i]) {
            list.splice(i, 1);
            selectUserIds = list;
          }
        }
      }
      return {
        ...state,
        selectUserIds
      };
    }

    /**
     * @description: 添加部门
     * @param {Object} data 新增的档案类型
     */
    case ADD_DEPARTMENT: {
      const departmentList = insertItemAfter(action.data, state.departmentList);

      return {
        ...state,
        departmentList,
        departmentTree: arrToTree(departmentList)
      };
    }

    /**
     * @description: 编辑部门
     * @param {Object} data 新增的档案类型
     */
    case EDIT_DEPARTMENT: {
      const { id, data } = action;
      const departmentList = editItemById(id, data, state.departmentList);
      console.log('EDIT_DEPARTMENT', id, departmentList);
      return {
        ...state,
        departmentList,
        departmentTree: arrToTree(departmentList)
      };
    }

    /**
     * @description: 添加档案类型
     * @param {Object} data 新增的档案类型
     */
    case DELETE_DEPARTMENT: {
      const departmentList = delItemByIdArray(action.ids, state.departmentList);

      return {
        ...state,
        departmentList,
        departmentTree: arrToTree(departmentList)
      };
    }

    /**
     * @description: 加载表格数据及相关数据
     * @param {*}
     * @return {*}
     */
    case SET_QUERY: {
      const userQuery = { ...state.userQuery };
      action.info.forEach(({ key, value }) => {
        userQuery[key] = value;
      });

      return {
        ...state,
        userQuery
      };
    }

    /**
     * @description: 添加档案类型
     * @param {Object} data 新增的档案类型
     */
    case SHOW_USER_LIST_LOADING:
      return {
        ...state,
        userListLoading: true
      };

    /**
     * @description: 添加档案类型
     * @param {Object} data 新增的档案类型
     */
    case SET_USER_LIST:
      return {
        ...state,
        userList: action.data.results,
        userTotal: action.data.total,
        userListLoading: false
      };

    /**
     * @description: 添加档案类型
     * @param {Object} data 新增的档案类型
     */
    case ADD_USER: {
      const type = action.saveType;
      const _list = state.userList;
      const data = action.data.user;
      const userList =
        type === 'add' ? insertItem(data, _list) : editItemById(data.id, data, _list);

      return {
        ...state,
        userList,
        userDialogVisible: false
      };
    }

    /**
     * @description: 添加档案类型
     * @param {Object} data 新增的档案类型
     */
    case DELETE_USER:
      return {
        ...state,
        userList: delItemByIdArray(action.ids, state.userList),
        selectUserIds: delItemByIdArray(action.srtIds, state.selectUserIds)
      };

    /**
     * @description: 清楚选中的用户
     */
    case CLEAR_SELECT_USER:
      return {
        ...state,
        selectUserIds: []
      };

    default:
      console.log('untouched', action);
      return state;
  }
};
